<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.taskName"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡逻区域" prop="patrolArea">
        <el-input
          v-model="queryParams.patrolArea"
          placeholder="请输入巡逻区域"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡逻路线" prop="patrolRoute">
        <el-input
          v-model="queryParams.patrolRoute"
          placeholder="请输入巡逻路线"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡逻频率" prop="patrolFrequency">
        <el-input
          v-model="queryParams.patrolFrequency"
          placeholder="请输入巡逻频率"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡逻开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择巡逻开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="巡逻结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择巡逻结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['zy_patrol_task:task:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zy_patrol_task:task:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zy_patrol_task:task:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['zy_patrol_task:task:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="任务名称" align="center" prop="taskName" />
      <el-table-column label="巡逻类型" align="center" prop="patrolType" />
      <el-table-column label="巡逻区域" align="center" prop="patrolArea" />
      <el-table-column label="巡逻路线" align="center" prop="patrolRoute" />
      <el-table-column label="巡逻频率" align="center" prop="patrolFrequency" />
      <el-table-column label="巡逻开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡逻结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="任务状态(0:未开始,1:进行中,2:已完成)" align="center" prop="status" />-->
      <el-table-column label="任务状态" align="center" prop="status">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">
            {{ getStatusText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['zy_patrol_task:task:edit']">修改</el-button>
          <el-button link type="success" icon="Check" @click="completeTask(scope.row)" v-hasPermi="['zy_patrol_task:task:complete']" v-if="scope.row.status !== '2'">已完成</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['zy_patrol_task:task:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改巡逻任务表对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="taskRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="巡逻区域" prop="patrolArea">
          <el-input v-model="form.patrolArea" placeholder="请输入巡逻区域" />
        </el-form-item>
        <el-form-item label="巡逻路线" prop="patrolRoute">
          <el-input v-model="form.patrolRoute" placeholder="请输入巡逻路线" />
        </el-form-item>
        <el-form-item label="巡逻频率" prop="patrolFrequency">
          <el-input v-model="form.patrolFrequency" placeholder="请输入巡逻频率" />
        </el-form-item>
        <el-form-item label="巡逻开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择巡逻开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="巡逻结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择巡逻结束时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Task">
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/zy_patrol_task/task"

const { proxy } = getCurrentInstance()

const taskList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    taskName: null,
    patrolType: null,
    patrolArea: null,
    patrolRoute: null,
    patrolFrequency: null,
    startTime: null,
    endTime: null,
    status: null,
  },
  rules: {
    taskName: [
      { required: true, message: "任务名称不能为空", trigger: "blur" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询巡逻任务表列表 */
function getList() {
  loading.value = true
  listTask(queryParams.value).then(response => {
    taskList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    taskName: null,
    patrolType: null,
    patrolArea: null,
    patrolRoute: null,
    patrolFrequency: null,
    startTime: null,
    endTime: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("taskRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加巡逻任务表"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getTask(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改巡逻任务表"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["taskRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTask(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addTask(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除巡逻任务表编号为"' + _ids + '"的数据项？').then(function() {
    return delTask(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 完成任务 */
function completeTask(row) {
  proxy.$modal.confirm('是否确认将任务"' + row.taskName + '"设置为已完成？').then(function() {
    const updateData = {
      id: row.id,
      status: 2
    };
    return updateTask(updateData);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("任务已完成");
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('zy_patrol_task/task/export', {
    ...queryParams.value
  }, `task_${new Date().getTime()}.xlsx`)
}

// 状态文本显示
function getStatusText(status) {
  // 处理空值和非数字情况
  if (status === null || status === undefined || status === '') {
    return '未知';
  }
  
  switch (String(status)) {
    case '0':
      return '未开始';
    case '1':
      return '进行中';
    case '2':
      return '已完成';
    default:
      return '未知';
  }
}

// 状态样式显示
function getStatusType(status) {
  // 处理空值和非数字情况
  if (status === null || status === undefined || status === '') {
    return 'danger';
  }
  
  switch (String(status)) {
    case '0':
      return 'info';
    case '1':
      return 'warning';
    case '2':
      return 'success';
    default:
      return 'danger';
  }
}

getList()
</script>
